package tmi.li.txwidgets.txclck;


import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import tmi.li.txwidgets.ColorPickerDialog;
import tmi.li.txwidgets.R;

public class TxClckConfiguration extends Activity {
	@SuppressWarnings("unused")
	private static final String TAG ="tmi.li.txwidgets.txclck.TxClckConfiguration";
	int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
	private TxClckWidget widget;
	private CharSequence[] appNames;

	public TxClckConfiguration() {
		super();
	}


	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		setResult(RESULT_CANCELED);
		setContentView(R.layout.txclck_config);

		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null) {
			mAppWidgetId = extras.getInt(
					AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
		}

		// If they gave us an intent without the widget id, just bail.
		if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
			finish();
		}

		//registering event listener

		Spinner s = (Spinner) findViewById(R.id.sFormat);
		@SuppressWarnings("rawtypes")
		ArrayAdapter adapter2 = ArrayAdapter.createFromResource(
				this, R.array.format_time, android.R.layout.simple_spinner_item);
		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s.setAdapter(adapter2);
		
		
		//COLOR
        TextView ed = (TextView)findViewById(R.id.tFLColor);
        ed.setOnClickListener(openColorDialog);
        ed = (TextView)findViewById(R.id.tFTColor);
        ed.setOnClickListener(openColorDialog);
        ed = (TextView)findViewById(R.id.tFBColor);
        ed.setOnClickListener(openColorDialog);
        ed = (TextView)findViewById(R.id.tBColor);
        ed.setOnClickListener(openColorDialog);
		
        findViewById(R.id.bAppName).setOnClickListener(mAppClickListener);
		findViewById(R.id.bClckSave).setOnClickListener(mSaveOnClickListener);

		widget = new TxClckWidget(getBaseContext());
		widget._format = "12h";
		widget._id = mAppWidgetId;
	}





	//onclick listener for AppSelector DIalog
	View.OnClickListener mAppClickListener = new View.OnClickListener() {
		public void onClick(View v) {
			//final Context context = AppWidgetConfigure.this;

			//saving the city name in the var city & starting the thread 
			openAppMenu();
		}
	};

	//onClick listener for the Save button
	View.OnClickListener mSaveOnClickListener = new View.OnClickListener() {
		public void onClick(View v) {

			Context context = getBaseContext();

			Intent resultValue = new Intent();
			resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
			setResult(RESULT_OK, resultValue);

			
			String test = (String)((Spinner)findViewById(R.id.sFormat)).getSelectedItem();
			if(test.contains("4")) {
				widget._format = "24h";
			}else {
				widget._format = "12h";
			}
			
			
			widget._flcolor = ((EditText)findViewById(R.id.eFLColor)).getText().toString();
			widget._ftcolor = ((EditText)findViewById(R.id.eFTColor)).getText().toString();
			widget._fbcolor = ((EditText)findViewById(R.id.eFBColor)).getText().toString();
			widget._bcolor = ((EditText)findViewById(R.id.eBColor)).getText().toString();
			
			widget.save();
			//updating the widget
			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
			TxClckProvider.updateWidget(context, appWidgetManager, mAppWidgetId);

			finish();

		}
	};


	//menu dialog for picking a app
	public void openAppMenu() {
		//IntentCategorys
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);;
		//Init Lists
		@SuppressWarnings("rawtypes")
		final List AppList = getPackageManager().queryIntentActivities(mainIntent, 0);
		//final CharSequence[] appPackageNames = new CharSequence[AppList.size()];
		appNames = new CharSequence[AppList.size()+2];
		appNames[0] = "none";
		appNames[1] = "update Clock";
		final ResolveInfo[] appInfos =  new ResolveInfo[AppList.size()+2];
		int i = 2;
		//Get appnames and packagenames
		for (Object item:  getPackageManager().queryIntentActivities( mainIntent, 0)) {
			appInfos[i] = (ResolveInfo)item;
			appNames[i] = getPackageManager().getApplicationLabel(appInfos[i].activityInfo.applicationInfo);
			i++;
		}

		//Create dialog.builder
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Pick a app");
		builder.setItems(appNames, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				TextView text = (TextView)findViewById(R.id.tAppName);
				text.setText(appNames[item]);	
				if(item == 0) {
					widget._appname = null;
				}else if(item == 1) {
					widget._appname = "update";
				}else {
					Toast.makeText(getApplicationContext(), "selected App: "+appNames[item], Toast.LENGTH_SHORT).show();	
					widget._appname = appInfos[item].activityInfo.packageName.toString()+"/"+appInfos[item].activityInfo.name.toString();
				}

			}
		});

		//Create dialog with builder
		AlertDialog alert = builder.create();

		//Display dialog
		alert.show();
	}


	View.OnClickListener openColorDialog = new View.OnClickListener() {

		public void onClick(View v) {
			final int ids = v.getId();
			final ColorPickerDialog clr = new ColorPickerDialog(v.getContext());
			clr.onSaveClicked = new View.OnClickListener() {

				public void onClick(View v) {
					EditText ed = (EditText)findViewById(ids-1);
					ed.setText(clr.getColorHex());
					TextView view = (TextView)findViewById(ids);
					view.setBackgroundColor(clr.getColor());
					clr.dismiss();
				}
			};
			clr.show();
		}
	};


}
